package org.xshop.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.xshop.model.TGoodsComment;
import org.xshop.service.ITGoodsCommentService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import top.ibase4j.core.base.AppBaseController;
import top.ibase4j.core.util.WebUtil;

/**
 * <p>
 * 商品评论 前端控制器
 * </p>
 *
 * @author ShenHuaJie
 * @since 2018-05-21
 */
@Controller
@RequestMapping("/app/goods")
@Api(tags = "GoodsComment", value = "商品评论接口", description = "商品评论接口")
public class TGoodsCommentController extends AppBaseController<TGoodsComment, ITGoodsCommentService> {
    @PostMapping(value = "/getCommentList.api")
    @ApiOperation(value = "商品评论列表", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object queryAll(HttpServletRequest request) {
        Map<String, Object> param = WebUtil.getParameter(request);
        Assert.notNull(param.get("goodsId"), "商品ID不能为空");
        Object result = service.queryAll(param);
        return setSuccessModelMap(result);
    }

    @PostMapping(value = "/getMyCommentList.api")
    @ApiOperation(value = "我的评论列表", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object queryMy(HttpServletRequest request) {
        Map<String, Object> param = WebUtil.getParameter(request);
        Long memberId = getCurrUser(request);
        Assert.notNull(memberId, "用户ID不能为空");
        param.put("createBy", memberId);
        Object result = service.queryAll(param);
        return setSuccessModelMap(result);
    }

    @PostMapping(value = "/getCommentDetail.api")
    @ApiOperation(value = "商品评论详情", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object get(HttpServletRequest request) {
        Map<String, Object> param = WebUtil.getParameter(request);
        Long memberId = getCurrUser(request);
        Assert.notNull(memberId, "用户ID不能为空");
        Assert.notNull(param.get("goodsId"), "商品ID不能为空");
        param.put("createBy", memberId);
        return query(param);
    }

    @PostMapping(value = "/addComment.api")
    @ApiOperation(value = "添加商品评论", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object update(HttpServletRequest request) {
        Map<String, Object> params = WebUtil.getParameter(request);
        Long memberId = getCurrUser(request);
        Assert.notNull(memberId, "用户ID不能为空");
        params.put("memberId", memberId);
        service.addComment(params);
        return setSuccessModelMap();
    }
}